﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyCommerce.ManagementBase.Common.Base.Common;
using MyCommerce.Presentation.UsersAndSecurity.IViews;
using MyCommerce.Presentation.UsersAndSecurity.Presenters;
using Innova.Microfinanzas.BusinessEntities;
using Innova.Microfinanzas.BusinessEntities.SystemEnums;
using Innova.FoundationFramework.DataTypes;
using Innova.Microfinanzas.Windows.Common.Views;

namespace MyCommerce.Windows.UsersAndSecurity.Views
{
    public partial class UserAdministrationView : BaseForm, IUserAdministrationView
    {
        private UserAdministrationPresenter pPresenter;

        public UserAdministrationView()
        {
            InitializeComponent();
            pPresenter = new UserAdministrationPresenter(this);
        }

        #region IUserAdministrationView Members

        public long UserId 
        {
            get
            {
                return Context.UserId;
            }
        }

        public List<User> ColUsersList
        {
            get
            {
                return (List<User>)gridUsersList.DataSource;
            }
            set
            {
                gridUsersList.DataSource = value;
                gridUsersList.DataBind();
            }
        }

        public User ObjUserSelected
        {
            get
            {
                if (gridUsersList.ActiveRow != null
                    && gridUsersList.ActiveRow.Selected)
                    return (User)gridUsersList.ActiveRow.ListObject;
                return null;
            }
        }

        public bool InvokeEditionView(User objUserToEdit)
        {
            bool result = false;
            Command commandForm = Context.GetCommand("UserCreateEdit");
            object formResult = Context.InvoqueCommandWithParams(commandForm, objUserToEdit, true);
            if (formResult != null)
                result = Convert.ToBoolean(formResult);
            return result;
        }

        public event GenericEventHandler<long> OnLoadView;

        public event EventHandler OnCreateUser;

        public event EventHandler OnGeneratedPassword;

        public event GenericEventHandler<User> OnEditUser;

        public event GenericEventHandler<User> OnDeleteUser;

        public event GenericEventHandler<List<string>> OnChangePassword;

        #endregion

        private void tsNewButtonToolStrip_Click(object sender, EventArgs e)
        {
            if (OnCreateUser != null)
                OnCreateUser(this, new EventArgs());
        }

        private void tsEditButtonToolStrip_Click(object sender, EventArgs e)
        {
            try
            {
                if (ObjUserSelected != null)
                {
                    if (OnEditUser != null)
                        OnEditUser(this, new GenericEventArgs<User>(ObjUserSelected));
                }
                else
                    Context.ShowInformationMessageBox("Debe seleccionar un usuario para poder editarlo.", "Eliminar usuario");
            }
            catch (Exception ex)
            {
                Context.HandleException(ex);
            }
        }

        private void tsDeleteButtonToolStrip_Click(object sender, EventArgs e)
        {
            try
            {
                if (ObjUserSelected != null)
                {
                    if (Context.ShowQuestionMessageBox("¿Desea eliminar a este usuario?", "Eliminar usuario") == System.Windows.Forms.DialogResult.Yes)
                    {
                        if (OnDeleteUser != null)
                        {
                            OnDeleteUser(this, new GenericEventArgs<User>(ObjUserSelected));
                            Context.ShowInformationMessageBox("El usuario se eliminó correctamente.", "Eliminar usuario");
                        }
                    }
                }
                else
                    Context.ShowInformationMessageBox("Debe seleccionar un usuario para poder eliminarlo.", "Eliminar usuario");
            }
            catch (Exception ex)
            {
                Context.HandleException(ex);
            }
        }

        private void tsCloseButtonToolStrip_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void UserAdministrationView_Load(object sender, EventArgs e)
        {
            if (OnLoadView != null)
                OnLoadView(this, new GenericEventArgs<long>(Context.UserId));
        }

        private void tsRegeneratedPassword_Click(object sender, EventArgs e)
        {
            try
            {
                if (ObjUserSelected != null)
                {
                    if (OnGeneratedPassword != null)
                    {
                        OnGeneratedPassword(this, new EventArgs());
                        Context.ShowInformationMessageBox("La contraseña fue generada correctamente. La nueva contraseña es: 123", "Administración de usuarios");
                    }
                }
                else
                    Context.ShowInformationMessageBox("Debe seleccionar un usuario para poder regenerar su contraseña.", "Administración de usuarios");
            }
            catch (Exception ex)
            {
                Context.HandleException(ex);
            }
        }

        private void tsChangePassword_Click(object sender, EventArgs e)
        {
            try
            {
                if (ObjUserSelected != null)
                {
                    if (OnChangePassword != null)
                    {
                        List<string> colData = UserChangePassword.ChangePassword();
                        if (colData != null
                            && colData.Count > 1)
                        {
                            OnChangePassword(this, new GenericEventArgs<List<string>>(colData));
                            Context.ShowInformationMessageBox("La contraseña fue cambiada correctamente.", "Administración de usuarios");
                        }
                    }
                }
                else
                    Context.ShowInformationMessageBox("Debe seleccionar un usuario para poder cambiar su contraseña.", "Administración de usuarios");
            }
            catch (Exception ex)
            {
                Context.HandleException(ex);
            }
        }
    }
}
